package com.ff.xyh.service.message.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.entity.message.RoomMessage;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.qo.message.RoomMessageQuery;
import com.ff.xyh.service.message.service.RoomMessageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 群聊消息表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@RestController
@RequestMapping("/message-service")
public class RoomMessageController {
    @Autowired
    private RoomMessageService roomMessageService;


    /**
     * 群聊消息 - 条件查询，包含分页
     * @param roomMessageQuery
     * @return
     */
    @PostMapping("get-room-message-condition")
    public ResultObject getRoomMessageCondition(
            @RequestBody RoomMessageQuery roomMessageQuery) {
        return roomMessageService
                .getRoomMessageCondition(roomMessageQuery);
    }

    @PostMapping("get-all-message-room")
    public ResultObject getAllMessageInRoom(
            @RequestParam(name = "roomId") String roomId) {
        QueryWrapper<RoomMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId)
                .lt("complaint_number", 10);
        List<RoomMessage> roomMessageList = roomMessageService.list(wrapper);
        if (roomMessageList == null || roomMessageList.isEmpty()) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("items", roomMessageList);
        }
    }


    @ApiOperation(value = "分页查询群聊信息")
    @PostMapping("get-page-message-room")
    public ResultObject getPageMessageInRoom(
            @RequestParam(name = "roomId") String roomId,
            @RequestParam(name = "currentPage") long currentPage,
            @RequestParam(name = "pageSize") long pageSize) {
        Page<RoomMessage> messagePage = new Page<>(currentPage, pageSize);
        QueryWrapper<RoomMessage> wrapper = new QueryWrapper<>();
//        wrapper.eq() 拼接条件
        wrapper.eq("room_id", roomId);
        roomMessageService.page(messagePage, wrapper);
        long total = messagePage.getTotal();
        List<RoomMessage> records = messagePage.getRecords();
        long pages = messagePage.getPages();

        if (records == null || records.isEmpty()) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok()
                    .data("total", total)
                    .data("current", currentPage)
                    .data("size", pageSize)
                    .data("pages", pages)
                    .data("items", records);
        }
    }
    @ApiOperation(value = "分页查询最先一条群聊信息前的消息")
    @PostMapping("get-page-message-room-before-id")
    public ResultObject getPageMessageInRoomBeforeMessage(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "roomId") String roomId,
            @RequestParam(name = "currentPage") long currentPage,
            @RequestParam(name = "pageSize") long pageSize) {
        Page<RoomMessage> messagePage = new Page<>(currentPage, pageSize);
        QueryWrapper<RoomMessage> wrapper = new QueryWrapper<>();
//        wrapper.eq() 拼接条件
        wrapper
                .lt("id", id)
                .eq("room_id", roomId);
        roomMessageService.page(messagePage, wrapper);
        long total = messagePage.getTotal();
        List<RoomMessage> records = messagePage.getRecords();
        long pages = messagePage.getPages();

        if (records == null || records.isEmpty()) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok()
                    .data("total", total)
                    .data("current", currentPage)
                    .data("size", pageSize)
                    .data("pages", pages)
                    .data("items", records);
        }
    }
    @ApiOperation(value = "分页查询群聊信息")
    @PostMapping("get-message-id")
    public ResultObject getMessageById(
            @RequestParam(name = "id") String id) {
        RoomMessage roomMessage = roomMessageService.getById(id);
        if (roomMessage == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            return ResultObject.ok()
                    .data("data", roomMessage);
        }
    }

    @PostMapping("add-message")
    public ResultObject addMessage(
            @RequestBody RoomMessage roomMessage) {
        if (roomMessageService.save(roomMessage)) {
            return ResultObject.ok().data("data", roomMessage);
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
        }
    }

    @PostMapping("delete-message")
    public ResultObject deleteMessageByIdAndUserId(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "userId") String userId) {
        QueryWrapper<RoomMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id)
                .eq("user_id", userId);
        RoomMessage roomMessage = roomMessageService.getById(id);
        if (roomMessage != null) {
            if (roomMessageService.remove(wrapper)) {
                return ResultObject.ok().data("data", roomMessage);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        } else {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        }
    }

}

